<template>
  <div class="login-container">
    <div class="animated-background">
      <div class="particle-container">
        <!-- 生成40个粒子，包括正方形和长方形 -->
        <div
          v-for="n in 40"
          :key="n"
          :class="['particle', n % 3 === 0 ? 'rectangle' : 'square']"
          :style="{
            '--delay': `${Math.random() * 15}s`,
            '--size': `${Math.random() * 15 + 5}px`,
            '--start-pos': `${Math.random() * 100}%`,
            '--rotation': `${Math.random() * 360}deg`,
          }"
        ></div>
      </div>
      <div class="gradient-animation"></div>
    </div>
    <div class="background-decoration">
      <div class="decoration-text primary top-left">知识改变命运</div>
      <div class="decoration-text primary top-right">教育创造未来</div>
      <div class="decoration-text primary bottom-left">学习永无止境</div>
      <div class="decoration-text primary bottom-right">追求卓越品质</div>

      <div class="decoration-text secondary" style="top: 30%; left: 15%">勤学</div>
      <div class="decoration-text secondary" style="top: 40%; right: 20%">笃行</div>
      <div class="decoration-text secondary" style="top: 60%; left: 25%">求实</div>
      <div class="decoration-text secondary" style="top: 70%; right: 15%">创新</div>

      <div class="decoration-text cloud" style="top: 25%; left: 40%">成长</div>
      <div class="decoration-text cloud" style="top: 45%; left: 60%">进步</div>
      <div class="decoration-text cloud" style="top: 65%; left: 35%">梦想</div>
      <div class="decoration-text cloud" style="top: 15%; left: 70%">奋斗</div>
      <div class="decoration-text cloud" style="top: 85%; left: 55%">希望</div>
      <div class="decoration-text cloud" style="top: 35%; left: 80%">努力</div>
      <div class="decoration-text cloud" style="top: 75%; left: 75%">探索</div>

      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
      <div class="circle circle-4"></div>
      <div class="circle circle-5"></div>

      <div class="decoration-line line-1"></div>
      <div class="decoration-line line-2"></div>
      <div class="decoration-line line-3"></div>
    </div>
    <div class="login-left">
      <div class="welcome-text">
        <h1>学生成绩管理系统</h1>
        <p>欢迎使用教育管理智能平台</p>
      </div>
    </div>
    <div class="login-right">
      <el-card class="login-card">
        <template #header>
          <div class="card-header">
            <img src="../assets/logo.svg" alt="Logo" class="logo" />
            <h2>用户登录</h2>
          </div>
        </template>

        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
          label-position="top"
          class="login-form"
        >


          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
              size="large"
            />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              show-password
              size="large"
            />
          </el-form-item>

          <div class="form-footer">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <el-link type="primary">忘记密码？</el-link>
          </div>

          <el-form-item>
            <el-button
              type="primary"
              class="w-full login-button"
              @click="handleLogin"
              :loading="loading"
              size="large"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { User, Lock} from '@element-plus/icons-vue'
import {ElMessage} from 'element-plus'
import {useUserStore} from "@/stores/userStores.js";
import request from "@/requests/request.js";
import router from "@/router/index.js";
import {storeToRefs} from "pinia";

const userStore = useUserStore();
const {setData, getInfoByToken} = userStore
const {token, role} = storeToRefs(userStore)

const loginFormRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
})

const rules = {
  role: [{ required: true, message: '请选择登录身份', trigger: 'change' }],
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const handleLogin = () => {
  if (!loginFormRef.value) return
  console.log("准备登录")
  localStorage.clear()
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      // TODO: 实现登录逻辑
      // 构建登录请求数据
      const loginData = {
        username: loginForm.username,
        password: loginForm.password,
      }

      // 发送登录请求
      loginRequest(loginData)
          .then(response => {
            console.log(response)
            console.log("登陆成功")
            // TODO 获取用户信息

            // 处理登录成功逻辑
            processLoginSuccess(response)
          })
          .catch(error => {
            // 处理登录失败逻辑
            processLoginFailure(error)
          })
          .finally(() => {
            loading.value = false
          })
    }
  })
}

// 登录请求函数
const loginRequest = async (loginData) => {
  try {
    const response = await request.post('/login', loginData)
    console.log("登陆成功了")
    return response.data
  } catch (error) {
    // 统一处理网络错误
    if (error.response) {
      throw new Error(error.response.data.message || '登录失败，请检查您的凭证')
    } else {
      throw new Error('网络连接失败，请稍后再试')
    }
  }
}
// 处理登录成功
const processLoginSuccess =async (data) => {
  localStorage.setItem('token', data.data.accessToken) // 保存在本地
  token.value = data.data.accessToken
  localStorage.setItem('token', data.data.accessToken)
  // 显示成功消息
  ElMessage.success({
    message: '登录成功，正在跳转...',
    duration: 1500
  })
  console.log("跳转")

  // TODO 获取用户信息
  const r = await getInfoByToken()
  console.log("角色" + JSON.stringify(r))
  // TODO
  if (!r){
    ElMessage.error({
      message: "未查询到用户信息，请重新登录"
    })
  }

}

// 处理登录失败
const processLoginFailure = (error) => {
  // 显示错误消息
  ElMessage.error({
    message: error.message,
    duration: 3000
  })
}
onMounted(() => {
  const lt = localStorage.getItem('token')
  console.log("本地token", lt)
  if (lt !== null && lt !== '') {
    token.value = lt
    getInfoByToken()
  }
})
</script>

<style scoped>
:root {
  --primary-color: #1890ff;
  --secondary-color: #1d39c4;
  --text-color: #1f2937;
  --border-color: rgba(255, 255, 255, 0.8);
  --shadow-color: rgba(0, 0, 0, 0.1);
  --gradient-primary: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  --gradient-light: linear-gradient(to right, rgba(24, 144, 255, 0.05), rgba(29, 57, 196, 0.05));
}

.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  position: relative;
  overflow: hidden;
  background-color: #f5f7fa;

}

.animated-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
  pointer-events: none;
}

.particle-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.particle {
  position: absolute;
  left: var(--start-pos);
  top: -20px;
  animation: fall 15s linear infinite;
  animation-delay: var(--delay);
  background: rgba(24, 144, 255, 0.2);
  box-shadow: 0 0 10px rgba(24, 144, 255, 0.1);
  backdrop-filter: blur(2px);
  transform: rotate(var(--rotation));
}

.particle.square {
  width: var(--size);
  height: var(--size);
  border-radius: 2px;
}

.particle.rectangle {
  width: calc(var(--size) * 3);
  height: var(--size);
  border-radius: 2px;
  background: linear-gradient(90deg, rgba(24, 144, 255, 0.2), rgba(29, 57, 196, 0.2));
}

@keyframes fall {
  0% {
    transform: translateY(-20px) rotate(var(--rotation));
    opacity: 0;
  }
  20% {
    opacity: 0.8;
    transform: translateY(20vh) rotate(calc(var(--rotation) + 45deg));
  }
  80% {
    opacity: 0.8;
    transform: translateY(80vh) rotate(calc(var(--rotation) + 135deg));
  }
  100% {
    transform: translateY(100vh) rotate(calc(var(--rotation) + 180deg));
    opacity: 0;
  }
}

/* 为方块添加悬浮效果 */
.particle:hover {
  background: rgba(24, 144, 255, 0.4);
  box-shadow: 0 0 15px rgba(24, 144, 255, 0.3);
}

/* 调整动画时间和延迟 */
.particle:nth-child(3n) {
  animation-duration: 20s;
}

.particle:nth-child(3n + 1) {
  animation-duration: 18s;
}

.particle:nth-child(3n + 2) {
  animation-duration: 16s;
}

.gradient-animation {
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background: linear-gradient(
    45deg,
    rgba(24, 144, 255, 0.05) 0%,
    rgba(29, 57, 196, 0.05) 25%,
    rgba(24, 144, 255, 0.05) 50%,
    rgba(29, 57, 196, 0.05) 75%,
    rgba(24, 144, 255, 0.05) 100%
  );
  /*animation: gradient-rotate 15s linear infinite;*/
}

@keyframes gradient-rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  overflow: hidden;
}

.decoration-text {
  position: absolute;
  font-family: 'Ma Shan Zheng', cursive;
  white-space: nowrap;
}

.decoration-text.primary {
  font-size: 72px;
  color: rgba(0, 0, 0, 0.035);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.decoration-text.secondary {
  font-size: 48px;
  color: rgba(24, 144, 255, 0.08);
  transform: rotate(-15deg);
}

.decoration-text.cloud {
  font-size: 32px;
  color: rgba(29, 57, 196, 0.15);
  animation: float-and-glow 6s ease-in-out infinite;
  animation-delay: var(--delay, 0s);
  text-shadow: 0 0 10px rgba(24, 144, 255, 0.2);
  transition: all 0.3s ease;
}

.decoration-text.cloud:hover {
  color: rgba(29, 57, 196, 0.3);
  text-shadow: 0 0 15px rgba(24, 144, 255, 0.4);
  transform: scale(1.1);
}

@keyframes float-and-glow {
  0%,
  100% {
    transform: translateY(0) rotate(var(--rotation, 0deg));
    text-shadow: 0 0 10px rgba(24, 144, 255, 0.2);
    color: rgba(29, 57, 196, 0.15);
  }
  50% {
    transform: translateY(-15px) rotate(var(--rotation, 0deg));
    text-shadow:
      0 0 20px rgba(24, 144, 255, 0.4),
      0 0 30px rgba(24, 144, 255, 0.2);
    color: rgba(29, 57, 196, 0.3);
  }
}

.decoration-text.cloud:nth-child(odd) {
  --rotation: 15deg;
  --delay: 1s;
}

.decoration-text.cloud:nth-child(even) {
  --rotation: -15deg;
  --delay: 2s;
}

.top-left {
  top: 5%;
  left: 5%;
  transform: rotate(-15deg);
}

.top-right {
  top: 15%;
  right: 5%;
  transform: rotate(15deg);
}

.bottom-left {
  bottom: 15%;
  left: 5%;
  transform: rotate(-15deg);
}

.bottom-right {
  bottom: 5%;
  right: 5%;
  transform: rotate(15deg);
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(24, 144, 255, 0.08) 0%, rgba(29, 57, 196, 0.08) 100%);
  animation: pulse 8s ease-in-out infinite;
  opacity: 0.5;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
}

.circle-2 {
  width: 400px;
  height: 400px;
  bottom: -200px;
  right: -200px;
}

.circle-3 {
  width: 200px;
  height: 200px;
  top: 50%;
  left: 30%;
  transform: translate(-50%, -50%);
}

.circle-4 {
  width: 150px;
  height: 150px;
  top: 20%;
  right: 25%;
}

.circle-5 {
  width: 250px;
  height: 250px;
  bottom: 15%;
  left: 20%;
}

.decoration-line {
  position: absolute;
  background: linear-gradient(90deg, rgba(24, 144, 255, 0.05) 0%, rgba(29, 57, 196, 0.05) 100%);
  height: 2px;
  transform-origin: left center;
  overflow: hidden;
}

.line-1 {
  width: 200px;
  top: 30%;
  left: 10%;
  transform: rotate(45deg);
}

.line-2 {
  width: 300px;
  top: 60%;
  right: 10%;
  transform: rotate(-45deg);
}

.line-3 {
  width: 250px;
  bottom: 25%;
  left: 40%;
  transform: rotate(30deg);
}

.decoration-line::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  animation: line-flow 3s linear infinite;
}

@keyframes line-flow {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

.login-left {
  flex: 0;
  background: linear-gradient(135deg, rgba(24, 144, 255, 0.95) 0%, rgba(29, 57, 196, 0.95) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  z-index: 3;
  box-shadow: 4px 0 15px rgba(0, 0, 0, 0.1);
}

.welcome-text {
  color: white;
  text-align: center;
  max-width: 600px;
  padding: 0 20px;
}

.welcome-text h1 {
  font-size: 3em;
  margin-bottom: 20px;
  font-weight: 600;
  letter-spacing: 2px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.welcome-text p {
  font-size: 1.4em;
  opacity: 0.9;
  letter-spacing: 1px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.login-right {
  flex: 1;
  min-width: 400px;
  max-width: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  z-index: 3;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  box-shadow: -8px 0 32px var(--shadow-color);
}

.login-card {
  width: 100%;
  border-radius: 12px;
  box-shadow: 0 8px 24px var(--shadow-color);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-color);
  overflow: hidden;
}

.card-header {
  text-align: center;
  padding: 24px 0;
  /*background: linear-gradient(to right, rgba(24, 144, 255, 0.05), rgba(29, 57, 196, 0.05));*/
}

.logo {
  width: 72px;
  height: 72px;
  margin-bottom: 16px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
}

h2 {
  margin: 0;
  color: #1f2937;
  font-weight: 600;
  font-size: 28px;
}

.login-form {
  padding: 24px 32px;
}

.w-full {
  width: 100%;
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  height: 44px;
  font-size: 16px;
}

.role-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;

}

.role-option .el-icon {
  font-size: 16px;
  color: var(--primary-color);
}

:deep(.el-card__header) {
  border-bottom: none;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: var(--text-color);
  font-size: 15px;
  line-height: 1.5;
  padding: 0 0 8px 0 !important;
  text-align: left !important;

}

:deep(.el-form-item) {
  margin-bottom: 24px;
  text-align: left;
}

:deep(.el-input__inner) {

  font-size: 14px;
  color: #2c3e50;
  height: 42px;
}

:deep(.el-select__input) {

}

:deep(.el-select-dropdown__item) {

  font-size: 14px;
  padding: 8px 12px;
}

:deep(.el-checkbox__label) {

  font-size: 14px;
  color: #2c3e50;
}

:deep(.el-button) {

  font-size: 15px;
  font-weight: 500;
  letter-spacing: 1px;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  padding: 0 12px;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--primary-color) !important;
}

:deep(.el-select .el-input__wrapper) {
  padding-left: 12px;
}

:deep(.el-input__prefix) {
  margin-right: 8px;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1) translate(0, 0);
  }
  50% {
    transform: scale(1.1) translate(10px, 10px);
  }
}
</style>
